package com.runa.rccprotocol.dto.feikong;

import com.runa.protocol.dto.IResponseData;
import com.runa.protocol.util.ByteUtil;
import com.runa.rccprotocol.dto.ConcentratorRequestData;
import com.runa.rccprotocol.dto.param.NBIoTValveParam;

public class FKNBRequestData extends ConcentratorRequestData {

    private byte[] bytes;

    public FKNBRequestData(String address) {
        super(address);
    }

    public void ctrlHouseValve(NBIoTValveParam param) {
        bytes = ByteUtil.hexStringToByte("697D7D690900730F335401980411143316240220200511000006117549093660EA111101141100151120991611009009101711000405181112031911350000000010231100000320110007001100165040351A110007001100164038410B110007001100161102031B1158514334221108041004000060022411153648000000006816");
        bytes[5] = (byte) param.getIndex();
        ByteUtil.tenstringToBcd(bytes, param.getAddress(), 8, false);
        ByteUtil.tenstringToBcd(bytes, param.getDate(), 14, false);
        ByteUtil.intToBcd(bytes, 23, param.getPeriod(), true);
        String[] ips = param.getIp().split("\\.");
        bytes[27] = (byte) Integer.parseInt(ips[0]);
        bytes[28] = (byte) Integer.parseInt(ips[1]);
        bytes[29] = (byte) Integer.parseInt(ips[2]);
        bytes[30] = (byte) Integer.parseInt(ips[3]);
        bytes[31] = (byte) (param.getPort() >> 8);
        bytes[32] = (byte) param.getPort();
        bytes[35] = (byte) (param.isLock() ? 0 : 1);
        bytes[38] = (byte) param.getModel();
        if (param.getModel() == 3) {
            ByteUtil.intToBcd(bytes, 46, (int) (param.getOpen() * 10), true);
        }
        if (param.getModel() == 0) {
            ByteUtil.intToBcd(bytes, 51, (int) (param.getTemp() * 10), true);
        }
        ByteUtil.intToBcd(bytes, 69, param.getInterval(), true);
        bytes[bytes.length - 2] = ByteUtil.sum(bytes, 4, bytes.length - 6);

        setHaveResult(true);
    }

    @Override
    public boolean check(IResponseData data) {
        return true;
    }

    @Override
    public byte[] getData() {
        return bytes;
    }

}
